package com.qs.rocketmq.mb.utils;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class ImageUtils {

    public static void pressImage(String targetPath, String destPath, Image blackImg, int left, int top, float alpha) {
        ImgUtil.pressImage(
                ImgUtil.read(FileUtil.file(targetPath)),
                FileUtil.file(destPath),
                blackImg, //水印图片
                left, //x坐标修正值。 默认在中间，偏移量相对于中间偏移
                top, //y坐标修正值。 默认在中间，偏移量相对于中间偏移
                alpha
        );
    }

    public static void pressImage(InputStream inputStream, String destPath, Image blackImg, int left, int top, float alpha) {
        ImgUtil.pressImage(
                ImgUtil.read(inputStream),
                FileUtil.file(destPath),
                blackImg, //水印图片
                left, //x坐标修正值。 默认在中间，偏移量相对于中间偏移
                top, //y坐标修正值。 默认在中间，偏移量相对于中间偏移
                alpha
        );
    }


    public static BufferedImage genBlackImage(int width, int height) {
        BufferedImage blackImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = blackImage.createGraphics();
        g2d.drawImage(blackImage, 0, 0, width, height, null);
//        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f)); //透明度设置开始
//        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); //透明度设置 结束
        g2d.setBackground(Color.white);
        g2d.clearRect(0, 0, width, height);
        return blackImage;
    }


    public static void main(String[] args) throws IOException {
//        BufferedImage blackImage = genBlackImage(200, 200);
//        String targetPath = "/Users/qinyupeng/Documents/target.png";
//        String destPath = "/Users/qinyupeng/Documents/target2.png";
//        pressImage(targetPath, destPath, blackImage, 0, 0, 1);

        File file = new File("/Users/qinyupeng/Documents/ceshi3.png");
        BufferedImage bufferedImage = ImageIO.read(file);
        System.out.println("-->>height=" + bufferedImage.getHeight());
        System.out.println("-->>width=" + bufferedImage.getWidth());

        File destFile = new File("/Users/qinyupeng/Documents/dest3.png");
        Rectangle rectangle = new Rectangle(300, 300);
        ImgUtil.cut(bufferedImage, destFile, rectangle);

    }
}
